package linear;

import java.util.Iterator;

/**
 * 双向链表
 */
public class TowWayLinkList<T> implements Iterable<T> {


    private class Node {
        public T data;
        public Node pre;
        public Node next;

        public Node(T data, Node pre, Node next) {
            this.data = data;
            this.pre = pre;
            this.next = next;
        }
    }

    /**
     * 头
     */
    private Node head;
    /**
     * 尾
     */
    private Node tail;
    /**
     * 数组长度
     */
    private int N;

    public TowWayLinkList() {
        head = new Node(null, null, null);
        tail = null;
        N = 0;
    }

    public void clear() {
        tail = null;
        head.next = null;
        head.pre = null;
        head.data = null;
        N = 0;
    }

    public boolean isEmpty() {
        return N == 0;
    }

    public int length() {
        return N;
    }

    public T get(int i) {
        checkIndex(i);
        // 从第0个开始找
        Node current = head.next;
        for (int j = 0; j < i; j++) {
            current = current.next;
        }
        return current.data;


    }

    public void insert(T t) {
        if (tail == null) {
            tail = new Node(t, head, null);
            head.next = tail;
        } else {
            Node oldNode = tail;
            Node node = new Node(t, oldNode, null);
            oldNode.next = node;
            tail = node;
        }
        N++;
    }

    public void insert(int i, T t) {
        checkIndex(i);

        Node pre = head;
        for (int j = 0; j < i; j++) {
            pre = pre.next;
        }
        Node current = pre.next;
        Node newNode = new Node(t, pre, current);

        pre.next = newNode;
        current.pre = newNode;
        N++;

    }

    public T remove(int i) {
        checkIndex(i);

        Node pre = head;
        for (int j = 0; j < i; j++) {
            pre = pre.next;
        }
        Node current = pre.next;
        Node next = current.next;

        pre.next = next;
        next.pre = pre;
        N--;
        return current.data;
    }

    private void checkIndex(int i) {
        if (i < 0 || i >= N) {
            throw new RuntimeException("位置不合法");
        }
    }

    public int indexOf(T t) {
        Node n = head;
        for (int i = 0; n.next != null; i++) {
            n = n.next;
            if (n.data.equals(t)) {
                return i;
            }
        }
        return -1;
    }

    public T getFirst() {

        if (isEmpty()) {
            return null;
        }
        return head.next.data;
    }

    public T getLast() {
        if (isEmpty()) {
            return null;
        }
        return tail.data;
    }

    @Override
    public Iterator<T> iterator() {
        return new TIterator();
    }

    private class TIterator implements Iterator<T> {
        private Node n = head;

        @Override
        public boolean hasNext() {
            return n.next != null;
        }

        @Override
        public T next() {
            n = n.next;
            return n.data;
        }
    }




    public static void main(String[] args) {
        TowWayLinkList<String> list = new TowWayLinkList<>();
        list.insert("乔峰");
        list.insert("虚竹");
        list.insert("段誉");
        list.insert(1, "鸠摩智");
        list.insert(3, "叶二娘");
        for (String str : list) {
            System.out.println(str);
        }
        System.out.println("----------------------");
        String tow = list.get(2);
        System.out.println(tow);
        System.out.println("-------------------------");
        String remove = list.remove(3);
        System.out.println(remove);
        System.out.println(list.length());
        System.out.println("--------------------");
        System.out.println(list.getFirst());
        System.out.println(list.getLast());


    }


}
